/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.sel1602.csp.service.impl;

import edu.fjut.sel1602.csp.Dao.AccountDao;
import edu.fjut.sel1602.csp.Dao.PetDao;
import edu.fjut.sel1602.csp.Dao.PetOwnerDao;
import edu.fjut.sel1602.csp.Dao.PetStoreDao;
import edu.fjut.sel1602.csp.Dao.impl.AccountDaoImpl;
import edu.fjut.sel1602.csp.Dao.impl.PetDaoOracleImpl;
import edu.fjut.sel1602.csp.Dao.impl.PetOwnerDaoOracleImpl;
import edu.fjut.sel1602.csp.Dao.impl.PetStoreDaoOracleImpl;
import edu.fjut.sel1602.csp.entity.Account;
import edu.fjut.sel1602.csp.entity.Pet;
import edu.fjut.sel1602.csp.entity.PetOwner;
import edu.fjut.sel1602.csp.entity.PetStore;
import edu.fjut.sel1602.csp.service.PetOwnerService;
import edu.fjut.sel1602.csp.service.PetStoreService;
import edu.fjut.sel1602.csp.util.BaseDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo
 */
public class PetOwnerServiceImpl implements PetOwnerService{
    
   
    
    Calendar c=Calendar.getInstance();
    SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
    PetDao pet =new PetDaoOracleImpl();
    PetOwnerDao owner=new PetOwnerDaoOracleImpl();
    PetStoreDao store=new PetStoreDaoOracleImpl();
    AccountDao account =new AccountDaoImpl();
    PetOwner po=new PetOwner();
   // PetOwnerService pos=new PetOwnerServiceImpl();
    PetStore ps=new PetStore();
   // PetStoreService pss=new PetStoreServiceImpl();
    Scanner input =new Scanner(System.in);
    String dbname=null;
    String dbstorename=null;
    int dbstorepassword=0;
    int dbpassword=0;
    int money;
    int ownerid;
    int petid;
    int price;
    int storeid;
    int balance;
    
    @Override
    public void buy() {
               Connection con=BaseDao.getConnection();
               String datetime=date.format(c.getTime());
               PetDaoOracleImpl pet=new PetDaoOracleImpl();
               PetStoreDaoOracleImpl store=new PetStoreDaoOracleImpl();
               PetOwnerDaoOracleImpl po=new PetOwnerDaoOracleImpl();
               List<PetStore> allstore=store.getAllStore();
               System.out.println("---------以下是库存列表:---------");
               System.out.println(ownerid);
               List<Pet> allpet=pet.getAllPet();
               for(Pet p:allpet)
               {
                   if(ownerid==p.getOwnerid())
                   System.out.println("第"+p.getId()+"个宠物"+",我的名字是"+p.getName()+"我是："+p.getTypename()+"，购买我要花："+p.getPrice()+"个元宝");
               }
               System.out.println("---------请选择要购买哪一个宠物，并输入序号:---------");
               petid=input.nextInt();
               for(Pet pp:allpet){
                   if(pp.getId()==petid){
                       price=pp.getPrice();
                       storeid=pp.getStoreid();
                   }
               }
               try {
               PreparedStatement pst=con.prepareStatement("update pet set OWNER_ID='"+ownerid+"',STORE_ID='0' where ID='"+petid+"'");
               pst.executeUpdate();
               } catch (SQLException ex) {
               Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
           }
               AccountDaoImpl ati=new AccountDaoImpl();
               Account account=new Account(1, petid, 0, ownerid, price ,datetime);
               ati.add(account);
               int nowmoney;
               nowmoney=money-price;
               try {
                   PreparedStatement pst=con.prepareStatement("update petowner set MONEY ='"+nowmoney+"' where ID='"+ownerid+"'");
                   pst.executeUpdate();
               } catch (SQLException ex) {
                   Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
               }
               for(PetStore petstore: allstore){
                   if(storeid==petstore.getID()){
                       balance=petstore.getBalance();
                   }
               }
               int nowbalance;
               nowbalance=balance+price;
               try{
                   PreparedStatement pst=con.prepareStatement("update petstore set BALANCE ='"+nowbalance+"' where ID='"+storeid+"'");
                   pst.executeUpdate();
               } catch (SQLException ex) {
                   Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
               }
               System.out.println("台账正确插入一条信息");
           BaseDao.closeConnection(con);
       }
  
 //   @Override
    public void sell() {
        Connection con=BaseDao.getConnection();
        PetOwnerDaoOracleImpl owner=new PetOwnerDaoOracleImpl();
        List<PetOwner> allowner=owner.getAllOwner();
        PetDaoOracleImpl pet=new PetDaoOracleImpl();
        List<Pet>allpet=pet.getAllPet();
        PetStoreDaoOracleImpl store=new PetStoreDaoOracleImpl();
        List<PetStore>allstore=store.getAllStore();
        System.out.println("---------我的宠物列表---------");
       // System.out.println(ownerid);
        
        for(Pet p:allpet){
            if(ownerid==p.getOwnerid())
                System.out.println("序号："+p.getId()+"，我的名字叫："+p.getName()+",我是："+p.getTypename());
        }
        System.out.println("---------选择要出售的宠物序号---------");
        int n=input.nextInt();
        for(Pet p:allpet){
            if(n==p.getId()){
                System.out.println("------您要出售的宠物信息如下------");
                System.out.println("宠物名字叫："+p.getName()+"，宠物类别是："+p.getTypename());
                price=p.getPrice();
            }
        } 
        System.out.println("请确认是否要卖出，y代表卖出，n代表不卖");
        String x=input.next();
        if("y".equals(x)){
            System.out.println("------下面是现有的宠物商店，请选择您要卖给的买家序号------");
            for(PetStore petstore:allstore){
                System.out.println("序号："+petstore.getID()+"，宠物商店的名字："+petstore.getName());
                balance=petstore.getBalance();
            }
            int nu=input.nextInt();
            try {
                PreparedStatement pst=con.prepareStatement("update pet set STORE_ID='"+nu+"',OWNER_ID='"+0+"' where ID='"+n+"'");
                pst.executeUpdate();
                } catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            int nowmoney;
            nowmoney=money+price;
            int nowbalance;
            nowbalance=balance-price;
            String dealtime=date.format(c.getTime());
            Account account=new Account(2, n, ownerid, 0, price, dealtime);
            AccountDaoImpl ai=new AccountDaoImpl();
            ai.add(account);
            try {
                PreparedStatement pst=con.prepareStatement("update petowner set MONEY='"+nowmoney+"'where ID='"+ownerid+"'");
                pst.executeUpdate();
            } catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            try {
                PreparedStatement ps = con.prepareStatement("update petstore set BALANCE='"+nowbalance+"'where ID='"+nu+"'");
                ps.executeUpdate();
            } 
            catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("台账正确插入一条信息");
            BaseDao.closeConnection(con);
        }
        else if("n".equals(x)){
            sell();
        }
    }
    
  //  @Override
    public void login(){
         Connection con=BaseDao.getConnection();
            System.out.println("请先登录，输入主人的姓名：");
            String name=input.next();
            System.out.println("请输入密码：");
            int pwd=input.nextInt();
            try{
                PreparedStatement pst=con.prepareStatement("select * from petowner where NAME='"+name+"'");
                ResultSet rs =pst.executeQuery();             
            while(rs.next()){
                dbname=rs.getString("NAME");
                dbpassword=rs.getInt("PASSWORD");
                money=rs.getInt("MONEY");
                ownerid=rs.getInt("ID");
            }
                System.out.println(ownerid);
                if(pwd==dbpassword)
                 {
                    System.out.println("------恭喜您，登录成功！------");
                    System.out.println("---------您的基本信息---------");
                    System.out.println("序号："+ownerid);
                    System.out.println("姓名："+name);
                    System.out.println("元宝数："+money);
                    System.out.println("登录成功，您可以进行买卖宠物，输入1为购买宠物，输入2为卖出宠物");
                    int t=input.nextInt();
                    //主人买宠物
                    if(t==1){
                         buy();
                    }
                    //主人卖出宠
                    if(t==2){
                         sell();
                    }
    }else {
                    System.out.println("账号或密码错误,请重新输入");                   
                }
            } catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
          BaseDao.closeConnection(con);
    }

}
  
